# coding=utf-8

import openpyxl
from difflib import SequenceMatcher
import leancloud
import unicodedata

class CommonClass():
    SameNameDatas = []
    SortDict = {}
    UnitDict = {}
    SellMakeUpDict = {}

    def __init__(self,*args, **kwargs):
        pass
        
    @staticmethod
    def GetSameNameDatas():
        if len(CommonClass.SameNameDatas) > 0:
            return CommonClass.SameNameDatas
        else:
            sameNameInfo = leancloud.Object.extend('SameName')
            query = sameNameInfo.query
            query.limit(1000)
            sameNameInfoQ = query.find()
            for elem in sameNameInfoQ:
                CommonClass.SameNameDatas.append([elem.get("ItemName"), elem.get("SameNameList")])
            print("SameName查询")
            return CommonClass.SameNameDatas
    
    @staticmethod
    def GetSortDict():
        if len(CommonClass.SortDict) > 0:
            return CommonClass.SortDict
        else:
            itemSortInfo = leancloud.Object.extend('ItemSort')
            query = itemSortInfo.query
            query.limit(1000)
            itemSortInfoQ = query.find()
            for elem in itemSortInfoQ:
                CommonClass.SortDict[elem.get("ItemName")] = elem.get("SortName")
            return CommonClass.SortDict

    @staticmethod
    def GetUnitDict():
        if len(CommonClass.UnitDict) > 0:
            return CommonClass.UnitDict
        else:
            itemUnitInfo = leancloud.Object.extend('ItemUnit')
            query = itemUnitInfo.query
            query.limit(1000)
            itemUnitInfoQ = query.find()
            for elem in itemUnitInfoQ:
                CommonClass.UnitDict[elem.get("ItemName")] = [elem.get("BigUnit"), elem.get("SmallUnit"), elem.get("SmallAmount")]
            return CommonClass.UnitDict

    @staticmethod
    def GetSellMakeUp():
        if len(CommonClass.SellMakeUpDict) > 0:
            return CommonClass.SellMakeUpDict
        else:
            sellMakeUpInfo = leancloud.Object.extend('SellMakeUp')
            query = sellMakeUpInfo.query
            query.limit(1000)
            sellMakeUpInfoQ = query.find()
            for elem in sellMakeUpInfoQ:
                CommonClass.SellMakeUpDict[elem.get("ItemName")] = elem.get("MakeUpList")
            return CommonClass.SellMakeUpDict


# 读取表格数据
def ReadExcel(excelName):
    try:
        workBook = openpyxl.load_workbook(excelName)
        return workBook.active, workBook
    except Exception as e:
        return None, None

#同义词转换
def SameNameConvert(sameName):
    #转换中文字符到英文
    sameName = unicodedata.normalize('NFKC', sameName)
    firstName = sameName
    SameNameDatas = CommonClass.GetSameNameDatas()
    for elem in SameNameDatas:
        sameNameList = elem[1]
        if sameName in sameNameList:
            firstName = elem[0]
            break
    return firstName

#获取物品分类配置
def GetSortDict():
    return CommonClass.GetSortDict()

#获取物品分类配置
def GetItemSortDict(itemName):
    sortDict = CommonClass.GetSortDict()
    if itemName in sortDict:
        return sortDict[itemName]
    else:
        return None

#获取销售成分配置
def GetSellMakeUp():
    return CommonClass.GetSellMakeUp()

#单位换算
def UnitTransform(itemName, count, unit):
    UnitDict = CommonClass.GetUnitDict()
    resultAmount = count
    resultUnit = unit
    if itemName in UnitDict:
        #输入为大单位
        if unit == UnitDict[itemName][0]:
            resultAmount = count * UnitDict[itemName][2]
        else:
            resultAmount = count
        resultUnit = UnitDict[itemName][1]
    return resultAmount, resultUnit

def MakeUpConvert(itemName, count, unit):
    resultList = []
    makeUpDict = GetSellMakeUp()
    if itemName in makeUpDict:
        for elem in makeUpDict[itemName]:
            resultAmount, resultUnit = Common.UnitTransform(elem[0], elem[1], elem[2])
            resultList.append([elem[0], resultAmount * count, resultUnit])
    else:
        resultList.append([itemName, count, unit])
    return resultList
